package com.bigworld.springbootbaseadmin.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bigworld.springbootbaseadmin.pojo.AjaxResult;
import com.bigworld.springbootbaseadmin.pojo.Goods;
import com.bigworld.springbootbaseadmin.service.IGoodsService;
import com.bigworld.springbootbaseadmin.service.impl.GoodsImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.Date;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;



    @PostMapping("/getOneInfo")
    public Goods getInfo() {
        QueryWrapper<Goods> queryWrapper = Wrappers.query();
        queryWrapper.eq("goods_id", 1);
        Goods member = goodsService.getOne(queryWrapper);
        System.out.println("member = " + member);
        return member;
    }

    @PostMapping("/test")
    public  void  test(){
        UpdateWrapper<Goods> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("goods_id", 1);
        Goods goods=new  Goods();
        goods.setCustomerId(118L);
        goods.setWeight(8);

        Goods goods2=new  Goods();
       BeanUtil.copyProperties(goods, goods2);
        boolean f=goodsService.update(goods2, updateWrapper);
        System.out.println("f = " + f);
    }

    @PostMapping("/test2")
    public  void  test2(){
       // UpdateWrapper<Goods> updateWrapper = new UpdateWrapper<>();
      //  updateWrapper.eq("goods_id", 1);
        Goods goods=new  Goods();
        goods.setCustomerId(118L);
        goods.setWeight(8);
        goods.setGoodsId(16L);
        Goods goods2=new  Goods();
        BeanUtil.copyProperties(goods, goods2);
        boolean f=goodsService.updateById(goods2);
        System.out.println("f = " + f);
    }

    @PostMapping("/test3")
    public AjaxResult save(@Validated @RequestBody Goods goods) {
        Goods goods2=new  Goods();
        BeanUtil.copyProperties(goods, goods2);
      boolean i=  goodsService.save(goods2);
        return toAjax(i==true?1:0);
    }


    @PostMapping("/test4")
    public AjaxResult update(@Validated @RequestBody Goods goods) {
        Goods goods2=new  Goods();
        BeanUtil.copyProperties(goods, goods2);
       goods2.setDate(new DateTime());
        UpdateWrapper<Goods> ew = new UpdateWrapper<>();
        ew.eq("goods_id", goods2.getGoodsId());
        boolean i=  goodsService.update(goods2,ew);
        return toAjax(i==true?1:0);
    }

    @PostMapping("/test5")
    public AjaxResult update2(@Validated @RequestBody Goods goods) {
        Goods goods2=new  Goods();
        goods2.setName(goods.getName());
        goods2.setWeight(goods.getWeight());
        goods2.setGoodsId(goods.getGoodsId());
        // goods2.setDate(new Date());
       // UpdateWrapper<Goods> ew = new UpdateWrapper<>();
      //  ew.eq("goods_id", goods2.getGoodsId());
        // 通过ID更新
        boolean i=  goodsService.updateById(goods2);
        return toAjax(i==true?1:0);
    }


    @PostMapping("/test6")
    public AjaxResult update6(@Validated @RequestBody Goods goods) {
        UpdateWrapper<Goods> updateWrapper = new UpdateWrapper<>();
        //这个就是更新的条件
        updateWrapper.eq("name","手机");

        Goods goods2 = new Goods();
        goods2.setWeight(goods.getWeight());

        boolean i= goodsService.update(goods2, updateWrapper);
        return toAjax(i==true?1:0);
    }


    /**
     * 响应返回结果
     *
     * @param rows 影响行数
     * @return 操作结果
     */
    protected AjaxResult toAjax(int rows)
    {
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }
}
